# -*- coding: utf-8 -*-
# __init__.py create by kumiler
# on 2022/7/26 8:56
# desc 车件分离明细&卸车&装车聚合
# from 张坤 https://ones.jtexpress.com.cn/wiki/#/team/5BXYuw3B/space/A3mD3eun/page/LabUVbpL
# **************************************************************************
#    Project Name:   疑似车件分离运单表-明细
#    Job Name:       jms_dm.dm_waybill_transfer_separated_detail_dt
#    Description :   根据装卸操作表运单之间的时间间隔时长是否大于3小时，
#    以及同一运单相邻站点之间的任务号是否异同来判断车件分离情况
#    Author :        鲁昆明
#    date：          2022/4/13
# **************************************************************************
#
# **************************************************************************
# modify by 鲁昆明 2022/05/10 更改脚本
# modify by 鲁昆明 on 2022/05/19 调整参数
# modify by 侯文龙 on 2023/04/10 调整参数
# **************************************************************************

from datetime import timedelta
from utils.operators.cluster_for_spark_sql_operator import SparkSqlOperator
from jms.dwd.oms.dwd_yl_oms_oms_waybill_incre_dt import jms_dwd__dwd_yl_oms_oms_waybill_incre_dt
from jms.dwd.tab.dwd_tab_barscan_unloading_base_dt import jms_dwd__dwd_tab_barscan_unloading_base_dt
from jms.dwd.tab.dwd_tab_barscan_loading_base_dt import jms_dwd__dwd_tab_barscan_loading_base_dt
from jms.dim.dim_network_whole_massage import jms_dim__dim_network_whole_massage
from jms.dwd.tms.dwd_tmsnew_shipment_stop_union_base_dt import jms_dwd__dwd_tmsnew_shipment_stop_union_base_dt
from jms.dwd.tab.dwd_tab_barscan_difficult_base_dt import jms_dwd__dwd_tab_barscan_difficult_base_dt
from jms.dim.tab.dim_tab_forfeit_config_new import jms_dim__dim_tab_forfeit_config_new
from jms.time_sensor.time_after_04_45 import time_after_04_45
##from utils.operators.spark_sql_operator import SparkSqlOperator


jms_dm__dm_waybill_transfer_separated_detail_dt = SparkSqlOperator(
        task_id='jms_dm__dm_waybill_transfer_separated_detail_dt',
        task_concurrency=1,
        depends_on_past=True,
        pool_slots=2,
        master='yarn',
        execution_timeout=timedelta(minutes=90),
        email=['houwenlong@jtexpress.com', 'yl_bigdata@yl-scm.com'],
        name='jms_dm__dm_waybill_transfer_separated_detail_dt_{{ execution_date | date_add(1) | cst_ds }}',
        sql='jms/dm/dm_waybill_transfer_separated/dm_waybill_transfer_separated_detail_dt.hql',
        driver_memory='8G',
        driver_cores=4,
        executor_cores=8,
        executor_memory='20G',
        num_executors=60,  # spark.dynamicAllocation.enabled 为 True 时，num_executors 表示最少 Executor 数
        conf={'spark.dynamicAllocation.enabled': 'true',  # 动态资源开启
              'spark.shuffle.service.enabled': 'true',  # 动态资源 Shuffle 服务开启
              'spark.dynamicAllocation.maxExecutors': 100,  # 动态资源最大扩容 Executor 数
              'spark.dynamicAllocation.initialExecutors': 60,  # 默认 Executor 数
              'spark.dynamicAllocation.cachedExecutorIdleTimeout': 600,  # 动态资源自动释放闲置 Executor 的超时时间(s)
              'spark.sql.sources.partitionOverwriteMode': 'dynamic',  # 允许删改已存在的分区
              'spark.sql.shuffle.partitions': 1200,
              'spark.hadoop.hive.exec.dynamic.partition.mode': 'true',
              'spark.network.timeout': 900,
              'spark.core.connection.ack.wait.timeout': 600,
              'spark.yarn.executor.memoryOverhead': 8192,
              },
        hiveconf={'hive.exec.dynamic.partition': 'true',  # 动态分区
                  'hive.exec.dynamic.partition.mode': 'nonstrict',
                  'hive.exec.max.dynamic.partitions': 20,  # 每天生成 20 个分区
                  'hive.exec.max.dynamic.partitions.pernode': 20,  # 每天生成 20 个分区
                  },
        yarn_queue='pro',
    )

jms_dm__dm_waybill_transfer_separated_detail_dt << [
    jms_dim__dim_network_whole_massage,
    jms_dwd__dwd_yl_oms_oms_waybill_incre_dt,
    jms_dwd__dwd_tab_barscan_unloading_base_dt,
    jms_dwd__dwd_tab_barscan_loading_base_dt,
    jms_dwd__dwd_tmsnew_shipment_stop_union_base_dt,
    jms_dwd__dwd_tab_barscan_difficult_base_dt,
    time_after_04_45
]

jms_dm__dm_waybill_transfer_separated_load_agg = SparkSqlOperator(
        task_id='jms_dm__dm_waybill_transfer_separated_load_agg',
        task_concurrency=1,
        depends_on_past=True,
        pool_slots=2,
        master='yarn',
        execution_timeout=timedelta(minutes=30),
        email=['houwenlong@jtexpress.com', 'yl_bigdata@yl-scm.com'],
        name='jms_dm__dm_waybill_transfer_separated_load_agg_{{ execution_date | date_add(1) | cst_ds }}',
        sql='jms/dm/dm_waybill_transfer_separated/dm_waybill_transfer_separated_load_agg.hql',
        driver_memory='4G',
        driver_cores=2,
        executor_cores=4,
        executor_memory='5G',
        num_executors=20,  # spark.dynamicAllocation.enabled 为 True 时，num_executors 表示最少 Executor 数
        conf={'spark.dynamicAllocation.enabled': 'true',  # 动态资源开启
              'spark.shuffle.service.enabled': 'true',  # 动态资源 Shuffle 服务开启
              'spark.dynamicAllocation.maxExecutors': 30,  # 动态资源最大扩容 Executor 数
              'spark.dynamicAllocation.cachedExecutorIdleTimeout': 300,  # 动态资源自动释放闲置 Executor 的超时时间(s)
              'spark.sql.sources.partitionOverwriteMode': 'dynamic',  # 允许删改已存在的分区
              'spark.sql.shuffle.partitions': 200,
              'spark.hadoop.hive.exec.dynamic.partition.mode': 'true',
              'spark.network.timeout': 900,
              'spark.core.connection.ack.wait.timeout': 300,
              'spark.yarn.executor.memoryOverhead': 8192,
              },
        hiveconf={'hive.exec.dynamic.partition': 'true',  # 动态分区
                  'hive.exec.dynamic.partition.mode': 'nonstrict',
                  'hive.exec.max.dynamic.partitions': 20,  # 每天生成 20 个分区
                  'hive.exec.max.dynamic.partitions.pernode': 20,  # 每天生成 20 个分区
                  },
        yarn_queue='pro',
    )

jms_dm__dm_waybill_transfer_separated_load_agg << jms_dm__dm_waybill_transfer_separated_detail_dt


jms_dm__dm_waybill_transfer_separated_unload_agg = SparkSqlOperator(
        task_id='jms_dm__dm_waybill_transfer_separated_unload_agg',
        task_concurrency=1,
        depends_on_past=True,
        pool_slots=2,
        master='yarn',
        execution_timeout=timedelta(minutes=30),
        email=['houwenlong@jtexpress.com', 'yl_bigdata@yl-scm.com'],
        name='jms_dm__dm_waybill_transfer_separated_unload_agg_{{ execution_date | date_add(1) | cst_ds }}',
        sql='jms/dm/dm_waybill_transfer_separated/dm_waybill_transfer_separated_unload_agg.hql',
        driver_memory='8G',
        driver_cores=2,
        executor_cores=4,
        executor_memory='5G',
        num_executors=20,  # spark.dynamicAllocation.enabled 为 True 时，num_executors 表示最少 Executor 数
        conf={'spark.dynamicAllocation.enabled': 'true',  # 动态资源开启
              'spark.shuffle.service.enabled': 'true',  # 动态资源 Shuffle 服务开启
              'spark.dynamicAllocation.maxExecutors': 30,  # 动态资源最大扩容 Executor 数
              'spark.dynamicAllocation.cachedExecutorIdleTimeout': 300,  # 动态资源自动释放闲置 Executor 的超时时间(s)
              'spark.sql.sources.partitionOverwriteMode': 'dynamic',  # 允许删改已存在的分区
              'spark.sql.shuffle.partitions': 200,
              'spark.hadoop.hive.exec.dynamic.partition.mode': 'true',
              'spark.network.timeout': 900,
              'spark.core.connection.ack.wait.timeout': 300,
              'spark.yarn.executor.memoryOverhead': 8192,
              },
        hiveconf={'hive.exec.dynamic.partition': 'true',  # 动态分区
                  'hive.exec.dynamic.partition.mode': 'nonstrict',
                  'hive.exec.max.dynamic.partitions': 20,  # 每天生成 20 个分区
                  'hive.exec.max.dynamic.partitions.pernode': 20,  # 每天生成 20 个分区
                  },
        yarn_queue='pro',
    )

jms_dm__dm_waybill_transfer_separated_unload_agg << [
   jms_dm__dm_waybill_transfer_separated_detail_dt,
   jms_dim__dim_tab_forfeit_config_new
]

